package com.gb.web.common.utils;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 敏感信息隐位工具类
 */
public class MaskUtil {

    /** 邮箱最小长度 */
    private static final int EMAIL_MIN_LEN = 2;

    private MaskUtil() {}

    /**
     * 邮箱隐位
     * @param email
     * @return
     */
    public static String maskEmail(String email) {
        if (!isEmail(email)) {
            return email;
        }
        int index = email.indexOf("@");
        String emailAccount = email.substring(0, index);
        int accountLength = emailAccount.length();
        String accountPrefix;
        String accountSuffix = emailAccount.substring(accountLength - 1);
        if (accountLength < EMAIL_MIN_LEN) {
            accountPrefix = emailAccount + emailAccount;
        } else {
            accountPrefix = emailAccount.substring(0, EMAIL_MIN_LEN);
        }
        return String.format("%s%s%s%s", accountPrefix, "***", accountSuffix, email.substring(index));
    }

    /**
     * 手机号隐位
     * @param mobile
     * @return
     */
    public static String maskMobile(String mobile) {
        if (!isMobile(mobile)) {
            return mobile;
        }
        String mobilePrefix = mobile.substring(0, 3);
        String mobileSuffix = mobile.substring(9);
        return String.format("%s%s%s", mobilePrefix, "****", mobileSuffix);
    }

    /**
     * 名称隐位
     * @param name
     * @return
     */
    public static String maskName(String name) {
        if (StringUtils.isNotEmpty(name)) {
            return "*" + name.substring(1);
        }
        return name;
    }

    /**
     * 银行卡号隐位
     * @param bankCardNo
     * @return
     */
    public static String maskBankCard(String bankCardNo) {
        if (isBankCardNo(bankCardNo)) {
            return "***" + bankCardNo.substring(bankCardNo.length() - 4);
        }
        return bankCardNo;
    }

    /**
     * 用户登录名隐位
     * @param userAlias
     * @return
     */
    public static String maskUserAlias(String userAlias) {
        if (StringUtils.isNotEmpty(userAlias)) {
            if (isEmail(userAlias)) {
                return maskEmail(userAlias);
            }
            if (isMobile(userAlias)) {
                return maskMobile(userAlias);
            }
        }
        return userAlias;
    }
    
    
    /**
     * 是否为邮箱
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        return match("^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\\.[a-zA-Z0-9]{2,6}$", email);
    }

    /**
     * 是否为手机号
     * @param mobile
     * @return
     */
    public static boolean isMobile(String mobile) {
        return match("^1[0-9]{10}$", mobile);
    }

    /**
     * 是否为银行卡号(^([1-9]{1})(\d{14}|\d{18})$;^\d{12,20}$\d{6}[- ]\d{10,13}$|^\d{4}[- ]\d{4}[- ]\d{4}[- ]\d{4,7}$)
     * @param bankCardNo
     * @return
     */
    public static boolean isBankCardNo(String bankCardNo) {
        return match("^\\d{12,20}$\\d{6}\\d{10,13}$|^\\d{4}\\d{4}\\d{4}\\d{4,7}$", bankCardNo);
    }

    /**
     * 模式匹配字符串格式
     * @param regex
     * @param str
     * @return
     */
    private static boolean match(String regex, String str) {
        if (StringUtils.isNotEmpty(regex) && StringUtils.isNotEmpty(str)) {
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(str);
            return matcher.matches();
        }
        return false;
    }
    
    public static String getRandomString(Integer num) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    public static String getRandomNum(Integer num) {
        String base = "0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < num; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }
    
    /**
     * 生成邀请码
     */
    public static String createShareCode() {
        int maxNum = 36;
        int i;
        int count = 0;
        char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while (count < 10) {
            i = Math.abs(r.nextInt(maxNum));
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count++;
            }
        }
        return pwd.toString();
    }


}
